//
//  VisitorViewController.swift
//  微博Swift
//
//  Created by apple on 15/9/22.
//  Copyright © 2015年 cc. All rights reserved.
//
// 访客模块

import UIKit

class VisitorViewController: UIView {
    
    // MARK: - 设置各个模块的访客视图 & 设置动画
    func setupInfo (imageName: String?, title:String) { // 不是首页
        titleLabel.text = title
        
        if imageName != nil {
            iconView.image = UIImage(named: imageName!)
            homeIconView.hidden = true
            sendSubviewToBack(maskIocnView)
        }else{  // 首页时
            // 设置动画
            startAnimation()
        }
    }
    
    private func startAnimation () {
        // 用基动画
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        
        anima.duration = 10
        anima.repeatCount = MAXFLOAT
        anima.toValue = M_PI * 2
        
        anima.removedOnCompletion = false
        
        iconView.layer.addAnimation(anima, forKey: nil)
    }
    
    // MARK: - 自定义初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    // fatalError("init(coder:) has not been implemented") 这一句会让程序崩溃
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        
        setupUI()
    }
    
    // MARK: -设置界面
    private func setupUI() {
        // 添加子控件
        addSubview(iconView)
        addSubview(maskIocnView)
        addSubview(homeIconView)
        addSubview(titleLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        
        // 便利 清除子控件的自动布局
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false;
        }

        
        // 设置约束
        // >> 底部图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        // >> 小房子图标
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY
            , relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // >> 文本框
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: titleLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        // >> 注册按钮
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Left, relatedBy: .Equal, toItem: titleLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 35))
        // >> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: titleLabel, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Right, relatedBy: .Equal, toItem: titleLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .Height, multiplier: 1.0, constant: 35))
        // >> 遮盖图标 使用 VFL 来约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[mask]-0-|", options: [], metrics: nil, views: ["mask":maskIocnView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[mask]-(margin)-[registBtn]", options: [], metrics: ["margin": -60], views: ["mask": maskIocnView,"registBtn": registBtn]))
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
    }
    
    // MARK: - 懒加载
    // 创建底部图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var maskIocnView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var homeIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var titleLabel: UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14)
    lazy var registBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "注册", fontSize: 14, color: UIColor.orangeColor())
    lazy var loginBtn: UIButton = UIButton(backImageName: "common_button_white_disable", title: "登录", fontSize: 14, color: UIColor.darkGrayColor())
}
